{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Checkpointing Tutorial\n",
    "\n",
    "To properly function, fast-graphrag mantains a state synchronised among different types of databases. It is highly unlikely, but it can happend that during any reading/writing operation any of these storages can get corrupted. So, we are introducing checkpointing to signficiantly reduce the impact of this unpleasant situation. To enable checkpointing, simply set `n_checkpoints = k`, with `k > 0`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fast_graphrag import GraphRAG\n",
    "\n",
    "DOMAIN = \"Analyze this story and identify the characters. Focus on how they interact with each other, the locations they explore, and their relationships.\"\n",
    "\n",
    "EXAMPLE_QUERIES = [\n",
    "    \"What is the significance of Christmas Eve in A Christmas Carol?\",\n",
    "    \"How does the setting of Victorian London contribute to the story's themes?\",\n",
    "    \"Describe the chain of events that leads to Scrooge's transformation.\",\n",
    "    \"How does Dickens use the different spirits (Past, Present, and Future) to guide Scrooge?\",\n",
    "    \"Why does Dickens choose to divide the story into \\\"staves\\\" rather than chapters?\"\n",
    "]\n",
    "\n",
    "ENTITY_TYPES = [\"Character\", \"Animal\", \"Place\", \"Object\", \"Activity\", \"Event\"]\n",
    "\n",
    "grag = GraphRAG(\n",
    "    working_dir=\"./book_example\",\n",
    "    n_checkpoints=2,  # Number of checkpoints to keep\n",
    "    domain=DOMAIN,\n",
    "    example_queries=\"\\n\".join(EXAMPLE_QUERIES),\n",
    "    entity_types=ENTITY_TYPES\n",
    ")\n",
    "\n",
    "with open(\"./book.txt\") as f:\n",
    "    grag.insert(f.read())\n",
    "\n",
    "print(grag.query(\"Who is Scrooge?\").response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DONE! Now the library will automatically keep in memory the `k` most recent checkpoints and rollback to them if necessary."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NOTES:\n",
    "- if you want to migrate a project from no checkpoints to checkpoints, simply set the flag and run a insert operation (even an empty one should do the job). Check that the checkpoint was created succesfully by querying the graph. If eveything worked correctly, you should see a new directory in you storage working dir (in the case above, it would be something like `./book_example/1731555907`). You can now safely remove all the files in the root dir `./book_example/*.*`.\n",
    "- if you want to stop using checkpoints, simply copy all the files from the most recent checkpoints folder in the root dir, delete all the \"number\" folders and unset `n_checkpoints`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
